package org.jeecg.modules.lab.exam.question.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.lab.exam.bankQuestion.mapper.LabExamBankQuestionMapper;
import org.jeecg.modules.lab.exam.entity.LabExamBankQuestion;
import org.jeecg.modules.lab.exam.entity.LabExamQuestion;
import org.jeecg.modules.lab.exam.entity.LabExamQuestionAnswer;
import org.jeecg.modules.lab.exam.question.mapper.LabExamQuestionAnswerMapper;
import org.jeecg.modules.lab.exam.question.mapper.LabExamQuestionMapper;
import org.jeecg.modules.lab.exam.question.service.ILabExamQuestionService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 考试试题信息表
 * @Author: jeecg-boot
 * @Date:   2023-08-24
 * @Version: V1.0
 */
@Service
public class LabExamQuestionServiceImpl extends ServiceImpl<LabExamQuestionMapper, LabExamQuestion> implements ILabExamQuestionService {

	@Autowired
	private LabExamQuestionMapper labExamQuestionMapper;
	@Autowired
	private LabExamQuestionAnswerMapper labExamQuestionAnswerMapper;
	@Autowired
	private LabExamBankQuestionMapper labExamBankQuestionMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(LabExamQuestion labExamQuestion, List<LabExamQuestionAnswer> labExamQuestionAnswerList) {
		labExamQuestionMapper.insert(labExamQuestion);
		if(labExamQuestionAnswerList!=null && labExamQuestionAnswerList.size()>0) {
			for(LabExamQuestionAnswer entity:labExamQuestionAnswerList) {
				//外键设置
				entity.setQuestionId(labExamQuestion.getId());
				labExamQuestionAnswerMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(LabExamQuestion labExamQuestion,List<LabExamQuestionAnswer> labExamQuestionAnswerList) {
		labExamQuestionMapper.updateById(labExamQuestion);
		
		//1.先删除子表数据
		labExamQuestionAnswerMapper.deleteByMainId(labExamQuestion.getId());
		
		//2.子表数据重新插入
		if(labExamQuestionAnswerList!=null && labExamQuestionAnswerList.size()>0) {
			for(LabExamQuestionAnswer entity:labExamQuestionAnswerList) {
				//外键设置
				entity.setQuestionId(labExamQuestion.getId());
				labExamQuestionAnswerMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		labExamQuestionAnswerMapper.deleteByMainId(id);
		labExamQuestionMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			labExamQuestionAnswerMapper.deleteByMainId(id.toString());
			labExamQuestionMapper.deleteById(id);
		}
	}

	@Override
	public void delBatchBank(String sId, String bId) {
		ArrayList<String> listIds=new ArrayList(Arrays.asList(sId.split(",")));
		QueryWrapper<LabExamBankQuestion> queryWrapper = new QueryWrapper<>();
		for (String qid : listIds) {
			queryWrapper.clear();
			queryWrapper.in("bank_id",bId);
			queryWrapper.in("question_id",qid);
			labExamBankQuestionMapper.delete(queryWrapper);
		}
	}

	@Override
	public void updateQuestion(String bankId) {
		labExamQuestionMapper.updateQuestion(bankId);
	}


}
